package com.vijay.extension.spring;

import com.vijay.extension.annotation.EnableSpringExtension;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

public class SpringExtensionImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        String name = EnableSpringExtension.class.getName();

        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(name);

        if (annotationAttributes == null || annotationAttributes.isEmpty()) return;

        AnnotationAttributes attributes = AnnotationAttributes.fromMap(annotationAttributes);

        String[] basePackages = attributes.getStringArray("basePackages");

        if (basePackages == null || basePackages.length == 0) return;


        SpringExtensionScanner scanner = new SpringExtensionScanner(registry);

        scanner.scan(basePackages);

    }
}
